<template>
  <div>
    <div v-if="!showTips">
      <div class="questionnaire">
        <div class="clearfix questionnaire-header">
          <h3 class="fl questionnaire-h">电子调查表</h3>
        </div>
      </div>     
      <div class="questionnaire">
        <el-tabs v-model="tabActive">
          <el-tab-pane v-for="(item, index) in tabList" :key="index" :label="' ' + item.label" :name="item.name">
            <component :is="item.name" :ref="item.name" @show-car-component="showCarFunc" :readonly="tempReadOnly"></component>
          </el-tab-pane>
        </el-tabs>
      </div>

    </div>
  </div>
</template>
<script>
  import BaseInfo from './tabs/baseInfo';
  import Finance from './tabs/finance';
  import PersonStatus from './tabs/personStatus';
  import ProductStatus from './tabs/productStatus';
  import Develop from './tabs/develop';
  import Achievement from './tabs/achievement';
  import Production from './tabs/production';
  import Environment from './tabs/environment';
  import Aptitude from './tabs/aptitude';
  import CarInfo from './tabs/carInfo';
  import SelectTree from '../../common/select-ext/select-tree';
  import qs from 'qs';
  import QuesTips from './quesTips';
  export default {
    components:{
      BaseInfo,
      Finance,
      PersonStatus,
      ProductStatus,
      Develop,
      Achievement,
      Production,
      Environment,
      Aptitude,
      CarInfo,
      SelectTree,
      QuesTips
    },
    data(){
      return {
        showTips: true,
        readyShow: false,
        tabActive: 'baseInfo',
        tabMap:{
          baseInfo:{
            label: '基本信息',
            name: 'baseInfo',
            showFlag: true
          },
          finance:{
            label: '财务信息',
            name: 'finance',
            showFlag: true
          },
          personStatus:{
            label: '人员情况',
            name: 'personStatus',
            showFlag: true
          },
          productStatus:{
            label: '产品情况',
            name: 'productStatus',
            showFlag: true
          },
          develop:{
            label: '开发能力',
            name: 'develop',
            showFlag: true
          },
          achievement:{
            label: '业绩客户',
            name: 'achievement',
            showFlag: true
          },
          production:{
            label: '生产检测设备',
            name: 'production',
            showFlag: true
          },
          environment:{
            label: '生产环境情况',
            name: 'environment',
            showFlag: true
          },
          aptitude:{
            label: '资质附件',
            name: 'aptitude',
            showFlag: true
          },
          carInfo:{
            label: '车辆资源',
            name: 'carInfo',
            showFlag: true
          }
        },
        vendorId: '',
        vendorInfo: {},
        dialogFormVisible: false,
        offeringCodeVisible: false,
        form:{
          dg_checkStatus:'1',
          dg_checkContent: '',
          immediatelyDev: 0
        },
        offeringCode:'',
        offeringName:'',
        vid: sessionStorage.getItem('vendorId'),
        pageFlag: null,
        messageTip: '',
        showExamine: false,
        orgType: ''
      };
    },
    props:{
      readonly:{
        type: Boolean,
        default: true
      }
    },
    created(){
      //审批初始化
      let _this = this;
      // Object.values(this.tabMap).forEach((item)=>{
      //   if (item.showFlag) {
      //     this.tabList.push(item);
      //   }
      // });
      this.tempReadOnly = this.readonly;
      if (this.vid && this.tempReadOnly) {
        this.axios.post(this.lib.config.host.vendorMgt + '/vendormgt/regist/questionary/toTab/' + this.vid).then((res)=>{
          this.showExamine = res.data.resultMap.checkStatus;
          this.showTips = false;
          this.readyShow = false;
          this.vendorId = _this.vid;
          this.vendorInfo = res.data.resultMap.vendorInfo;
          this.orgType = this.vendorInfo.cmpNature;
          this.$nextTick(()=>{
            this.myInitData('baseInfo',res.data.resultMap.vendorInfo);
            this.myInitData('finance',res.data.resultMap.vendorInfo);
            this.myInitData('personStatus',res.data.resultMap.vendorInfo);
            this.myInitData('productStatus',_this.vid);
            this.myInitData('develop',_this.vid);
            this.myInitData('achievement',_this.vid);
            this.myInitData('production',_this.vid);
            this.myInitData('environment',_this.vid);
            this.myInitData('aptitude',_this.vid);
            this.myInitData('carInfo',_this.vid);
          });
        });

        this.axios.post(this.lib.config.host.vendorMgt+'/vendormgt/vendorcategory/query/' + this.vid).then((res)=>{
          if (res.data.suuccess) {
            _this.offeringCode = res.data.resultMap.categoryList.toString().replace('[','').replace(']','');
          }
        });

        // 编辑初始化
      } else {
        this.editInit();
      }
    },
    computed:{
      tabList:function () {
        let list = [];
        Object.values(this.tabMap).forEach((item)=>{
          if (item.showFlag) {
            list.push(item);
          }
        });
        return list;
      }
    },
    methods:{
      editInit(){
        this.axios.post(this.lib.config.host.vendorMgt+'/vendormgt/regist/questionary/toTab').then((res)=>{
          if (res.data.resultMap.message !== '') {
            this.messageTip = res.data.resultMap.message;
          }
          this.pageFlag = res.data.resultMap.page;
          if (res.data.resultMap.page === 3 || res.data.resultMap.page === 1) {
            this.tempReadOnly = true;
          }
          this.showTips = res.data.resultMap.page === 0;
          this.readyShow = res.data.resultMap.page === 0;
          this.vendorId = res.data.resultMap.vendorId;
          this.vendorInfo = res.data.resultMap.vendorInfo;
          this.orgType = this.vendorInfo.cmpNature;
          if (!this.showTips && !this.readyShow) {
            this.compontentInit();
          }
        });
      },
      compontentInit(){
        this.showTips = false;
        this.readyShow = false;
        this.$nextTick(()=>{
          if (this.messageTip !== '') {
            this.$message({
                message: this.messageTip,
                type: 'warning',
                duration: 0,
                showClose: true
            });

          }
          this.myInitData('baseInfo', this.vendorInfo);
          this.myInitData('finance',this.vendorInfo);
          this.myInitData('personStatus',this.vendorInfo);
          this.myInitData('productStatus',this.vendorId);
          this.myInitData('develop',this.vendorId);
          this.myInitData('achievement',this.vendorId);
          this.myInitData('production',this.vendorId);
          this.myInitData('environment',this.vendorId);
          this.myInitData('aptitude',this.vendorId);
        });
      },
      myInitData(name, param){
        if (this.tabMap[name].showFlag) {
          this.$refs[name][0].initData(param);
        }
      },
      showCarFunc(orgType){
        this.orgType = orgType;
        for (let key in this.tabMap) {
          this.tabMap[key].showFlag = true;
        }
        if (this.orgType === 'ORG_TYPE_100302') { //贸易代理
          this.tabMap.develop.showFlag = false;
          this.tabMap.production.showFlag = false;
          this.tabMap.environment.showFlag = false;
          this.tabMap.carInfo.showFlag = false;
        } else if (this.orgType === 'ORG_TYPE_100304') { //物流仓储
          this.tabMap.production.showFlag = false;
        } else if (this.orgType === 'ORG_TYPE_100303') { //服务提供
          this.tabMap.production.showFlag = false;
          this.tabMap.carInfo.showFlag = false;
        }
      },
      tempSave(){
        this.editData(false, (result)=>{
          this.axios.post(this.lib.config.host.vendorMgt + '/vendormgt/regist/questionary/save', result.data, {
            headers: {
              'Content-Type': 'multipart/form-data'
            }
          }).then((res) => {
            //if (res.data.success)请不要删除，必须后台不报验证错误，才提示成功
            if (res.data.success) {
              this.$message({
              type: 'success',
              message: '保存成功!'
            });
            }
          });
        });
      },
      save(){
        this.editData(true, (result)=>{
          if (!result.check) {
            this.$message.info('请进一步完善企业信息，即可成为我司潜在供应商！');
            return false;
          } else {
            this.axios.post(this.lib.config.host.vendorMgt + '/vendormgt/regist/questionary/commit', result.data, {
              headers: {
                'Content-Type': 'multipart/form-data'
              }
            }).then((res) => {
              if (res.data.success) {
                this.$message({
                  type: 'success',
                  message: '保存成功!'
                });
                this.editInit();
              }
            });
          }
        });
      },
      editData(isCheck, callback){
        let data = {};
        let flag = true;
        let n = 0;
        let tempTab = [];
        let _this = this;
        this.tabList.forEach((tab, i)=>{
          this.$refs[tab.name][0].getData(isCheck).then((result)=>{
            n++;
            if (!result.check) {
              flag = false;
              tempTab.splice(n, 0 ,tab.name);
            }
            data =  Object.assign(data, result.data);
            if (n === _this.tabList.length) { //同步判断，调用回调
              let formData = new FormData();
              for (let key in data) {
                if (data[key] && data[key] !== '') {
                  formData.append(key, data[key]);
                }
              }
              if (flag) {
                callback({
                  data: formData,
                  check: true
                });
              } else {
                for (let i = 0; i < tempTab.length; i++) {
                  if (tempTab[i] && tempTab[i] !== '') {
                    this.tabActive = tempTab[i];
                    break;
                  }
                }
                callback({
                  data: formData,
                  check: false
                });
              }
            }
          });
        });
      },
      offeringSubmit(){
        this.axios.post(this.lib.config.host.vendorMgt + '/vendormgt/vendorcategory/update',qs.stringify({
            vendorId: this.vid,
            categorys: this.offeringCode
          })
        ).then((response)=>{
          if(response.data.success==false){
                this.$message(response.data.message);
              }else{
                this.offeringCodeVisible=false;
                this.$message('操作成功');
              }
        });
      },
      examineSubmit(){
        let _this = this;
        this.axios.post(this.lib.config.host.vendorMgt + '/vendormgt/questionary/check/' + this.vid, qs.stringify(this.form)).then((res)=>{
          if (res.data.success) {
            if (_this.form.dg_checkStatus === '1') {
              this.axios.post(this.lib.config.host.system + '/system/rolemgt/roles/addRole', qs.stringify({
                  roleId: '-1',
                  userList: res.data.resultMap.userList.toString()
              })).then((result)=>{
                this.axios.post(this.lib.config.host.vendorMgt + '/vendormgt/vendorcategory/update',qs.stringify({
                  vendorId: _this.vid,
                  categorys: _this.offeringCode
                })).then((response)=>{
                  window.close();
                });
              });
            } else {
              window.close();
            }
          }
        });
      },
      logout(){
        this.$message.closeAll();
        this.axios.post(this.lib.config.host.system + "/system/logout").then((res) => {
          this.$store.commit('updateSecurityId','');
          localStorage.removeItem('securityId');
          this.$router.push({name:'login'});
        });
      }
    }
  }
</script>
<style>
  .questionnaire .dialog-form .el-form-item .el-form-item__content{
    padding-left: 0;
  }

  .questionnaire .dialog-form .el-form-item{
    float:none;
  }

  .questionnaire .tab_content{
    height: 100%;
    min-height: 400px;
  }

  .questionnaire .el-tabs__nav-wrap.is-top{
    padding:0 20px;
  }
</style>
